package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.model.LocalStoreInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 本地商城
 * Created by huyang on 2016/1/11 0011.
 */
public class LocalStoreAdapter extends BaseAdapter {

    private Context context;
    private List<LocalStoreInfo> data;

    public LocalStoreAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public LocalStoreAdapter(Context context, List<LocalStoreInfo> data) {
        this.context = context;
        this.data = data;
    }

    public void setData(List<LocalStoreInfo> data) {
        this.data.clear();
        this.data.addAll(data);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LocalStoreInfo info = data.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_localstore, null);
            holder = new ViewHolder();
            holder.tv_storename = (TextView) convertView.findViewById(R.id.tv_storename);
            holder.tv_province = (TextView) convertView.findViewById(R.id.tv_province);
            holder.tv_city = (TextView) convertView.findViewById(R.id.tv_city);
            holder.tv_area = (TextView) convertView.findViewById(R.id.tv_area);
            holder.iv_store = (ImageView) convertView.findViewById(R.id.iv_store);
//            holder.iv_diamonds= (ImageView) convertView.findViewById(R.id.iv_diamonds);
            holder.mLinear_start = (LinearLayout) convertView.findViewById(R.id.linear_start);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_storename.setText(info.getStoreName());
        holder.tv_province.setText(info.getProvince());
        holder.tv_city.setText(info.getCity());
        holder.tv_area.setText(info.getArea());

        int tsIntegral=info.getTsIntegral();
        if (tsIntegral <= 250) {
            info.setImgRes(R.mipmap.already_collect);
            if (tsIntegral < 4) {
                info.setStartCount(0);
            } else if (tsIntegral >= 4 && tsIntegral <= 10) {
                info.setStartCount(1);
            } else if (tsIntegral >= 11 && tsIntegral <= 40) {
                info.setStartCount(2);
            } else if (tsIntegral >= 41 && tsIntegral <= 90) {
                info.setStartCount(3);
            } else if (tsIntegral >= 91 && tsIntegral <= 150) {
                info.setStartCount(4);
            } else if (tsIntegral >= 151 && tsIntegral <= 250) {
                info.setStartCount(5);
            }
        } else if (tsIntegral > 250 && tsIntegral <= 10000) {
            info.setImgRes(R.mipmap.zuanshi);
            if (tsIntegral <= 500) {
                info.setStartCount(1);
            } else if (tsIntegral >= 501 && tsIntegral <= 1000) {
                info.setStartCount(2);
            } else if (tsIntegral >= 1001 && tsIntegral <= 2000) {
                info.setStartCount(3);
            } else if (tsIntegral >= 2001 && tsIntegral <= 5000) {
                info.setStartCount(4);
            } else if (tsIntegral >= 5001 && tsIntegral <= 10000) {
                info.setStartCount(5);
            }
        } else if (tsIntegral > 10000 && tsIntegral <= 500000) {
            info.setImgRes(R.mipmap.sh_a2);
            if (tsIntegral <= 20000) {
                info.setStartCount(1);
            } else if (tsIntegral >= 20001 && tsIntegral <= 50000) {
                info.setStartCount(2);
            } else if (tsIntegral >= 50001 && tsIntegral <= 100000) {
                info.setStartCount(3);
            } else if (tsIntegral >= 100001 && tsIntegral <= 200000) {
                info.setStartCount(4);
            } else if (tsIntegral >= 200001 && tsIntegral <= 500000) {
                info.setStartCount(5);
            }
        } else if (tsIntegral > 500000 && tsIntegral <= 10000000) {
            info.setImgRes(R.mipmap.sh_a3);
            if (tsIntegral <= 1000000) {
                info.setStartCount(1);
            } else if (tsIntegral >= 1000001 && tsIntegral <= 2000000) {
                info.setStartCount(2);
            } else if (tsIntegral >= 2000001 && tsIntegral <= 5000000) {
                info.setStartCount(3);
            } else if (tsIntegral >= 5000001 && tsIntegral <= 10000000) {
                info.setStartCount(4);
            } else if (tsIntegral >= 10000001) {
                info.setStartCount(5);
            }
        }
        //动态生成店铺的星级
        holder.mLinear_start.removeAllViews();
        for (int i = 0; i < info.getStartCount(); i++) {
            ImageView imageView = new ImageView(context);
            imageView.setPadding(5,0,5,0);
            imageView.setImageResource(info.getImgRes());
            holder.mLinear_start.addView(imageView);
        }


        ImageLoader.getInstance().displayImage(info.getTsLogo(), holder.iv_store);
        return convertView;
    }

    class ViewHolder {
        TextView tv_storename, tv_province, tv_city, tv_area;
        ImageView iv_store, iv_diamonds;
        LinearLayout mLinear_start;
    }
}